package io.network;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.URL;

public class SocketTyper {

	/**
	 * @param args
	 * @throws IOException
	 * @throws
	 */
	public static void main(String[] args) throws IOException {
		if (args.length != 1) {
			throw new IllegalArgumentException("Usage: java URLTyper url");
		}
		URL url = new URL(args[0]);
		if (url.getProtocol().equalsIgnoreCase("http")) {
			System.err.println("Sorry, " + url.getProtocol()
					+ " is not supported");
			return;
		}
		String host = url.getHost();
		int port = url.getPort();
		String file = url.getFile();
		if (file == null) {
			file = "/";
		}
		if (port <= 0) {
			port = 80;
		}
		Socket s = null;
		s = new Socket(host, port);
		String request = "GET " + file + " HTTP/1.1\r\n"
				+ "User-Agent: SocketTyper\r\n" + "Accept: text/*\r\n"
				+ "Host: " + host + "\r\n" + "\r\n";
		byte[] b = request.getBytes("US-ASCII");
		OutputStream out = s.getOutputStream();
		InputStream in = s.getInputStream();
		out.write(b);
		out.flush();
		for (int c = in.read(); c != -1; c = in.read()) {
			System.out.write(c);
		}

	}

}
